package com.chixing.day10.day10_Homew;

import com.sun.org.apache.xerces.internal.xs.StringList;
import org.w3c.dom.Node;

/**
 * 链式结构的容器,用于存放字符串
 */
public class LinkString {
    private Node first;
    private Node last;
    private int size;// 已有元素个数
    private int modCount;  // 整个该容器结构上发生修改的次数，结构上（添加、删除元素）
    //防止快速迭代失败(防止在遍历的时候，有元素删除或添加 而导致 遍历不正确)

    public void addLast(String element){
        if(first ==null) {
            first = new Node(null, element, null);
            last = first;
        }else {
            Node oldLast = last;
            last = new Node(oldLast, element, null);
            oldLast.next = last;
        }
        size++;
        modCount++;
    }

    private class Node{
        String value;  // 元素值本身
        Node previous;//前一个元素
        Node next;//后一个元素

        private Node(Node previous,String value,Node next){
            this.value = value;
            this.previous = previous;
            this.next = next;
        }
    }

    public void show(){
        Node node = first;
        while (node != null) {
            System.out.println(node.value);
            node = node.next;
        }
    }
}
